package com.bookshop.listener;

import com.bookshop.scheduler.JobDetailAndTriggerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * ContextRefreshedEvent是容器初始化后会发送的时间
 * @author g2l
 * @create 2024-01-19 9:13
 */
@Slf4j
@Component
public class QuartzJobListener implements ApplicationListener<ContextRefreshedEvent> {

    @Resource
    private JobDetailAndTriggerService jobDetailAndTriggerService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 在应用程序上下文刷新完成后触发，此时可以启动Quartz任务
        try {
            log.info("监听器中启动quartz任务");
            jobDetailAndTriggerService.InitBloomFilterQuartz();
        } catch (SchedulerException e) {
            // 把异常的详细信息打印到日志中
            log.error(e.getMessage(), e);
        }
    }
}